package com.yangguang.huahui.mvp.presenter;

import com.google.gson.Gson;
import com.yangguang.huahui.entity.PersonInfo;
import com.yangguang.huahui.entity.SelectData;
import com.yangguang.huahui.entity.Upload;
import com.yangguang.huahui.mvp.model.CommonModel;
import com.yangguang.huahui.mvp.model.UploadModel;
import com.yangguang.huahui.mvp.view.PersonInfoView;
import com.yangguang.huahui.rx.RxManager;
import com.yangguang.huahui.rx.RxSubscriber;
import com.yangguang.huahui.utils.LogUtil;

import java.io.File;
import java.io.IOException;

import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2017/3/6.
 */

public class PersonInfoPresenter extends BasePresenter<PersonInfoView>{
    CommonModel model;
    UploadModel uploadModel;

    public PersonInfoPresenter() {
        model = new CommonModel();
        uploadModel = new UploadModel();
    }
    public void getInfo(String uid){
        mSubscription = RxManager.getInstance().doSubscribe(model.getInfo(uid), new RxSubscriber<PersonInfo>(false) {
            @Override
            protected void _onNext(PersonInfo personInfo) {
                mView.onSuccess(personInfo);
            }

            @Override
            protected void _onError() {
                mView.onError();
            }
        });
    }

    public void editInfo(String uid, String paramKey, String paramValue){
        mSubscription = RxManager.getInstance().doSubscribe(model.editInfo(uid, paramKey, paramValue), new RxSubscriber<PersonInfo>(false) {
            @Override
            protected void _onNext(PersonInfo personInfo) {
                mView.onSuccess(personInfo);
            }

            @Override
            protected void _onError()  {
                mView.onError();
            }
        });
    }

    /**
     * 上传头像
     * @param file
     */
    public void uploadFile(File file) {
        mSubscription = RxManager.getInstance().doSubscribe(uploadModel.uploadFile(file), new RxSubscriber<ResponseBody>(false) {
            @Override
            protected void _onNext(ResponseBody responseBody) {
                try {
                    String result = responseBody.string();
                    LogUtil.e(result, true);
                    mView.onUploadImg(new Gson().fromJson(result, Upload.class));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void _onError() {
                mView.onError();
            }
        });
    }


}
